package com.empire.module.rainbow.service.diypage;

import com.empire.module.rainbow.controller.app.diy.vo.AppRainbowDiyPagePageReqVO;
import com.empire.module.rainbow.controller.app.diy.vo.AppRainbowDiyPageSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.empire.module.rainbow.dal.dataobject.diypage.RainbowDiyPageDO;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.util.object.BeanUtils;

import com.empire.module.rainbow.dal.mysql.diypage.RainbowDiyPageMapper;

import java.util.List;

import static com.empire.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.empire.module.rainbow.enums.ErrorCodeConstants.*;

/**
 * 六彩-装修页面 Service 实现类
 *
 * @author wudi
 */
@Service
@Validated
public class RainbowDiyPageServiceImpl implements RainbowDiyPageService {

    @Resource
    private RainbowDiyPageMapper rainbowDiyPageMapper;

    @Override
    public Long createDiyPage(AppRainbowDiyPageSaveReqVO createReqVO) {
        // 插入
        RainbowDiyPageDO diyPage = BeanUtils.toBean(createReqVO, RainbowDiyPageDO.class);
        rainbowDiyPageMapper.insert(diyPage);
        // 返回
        return diyPage.getId();
    }

    @Override
    public void updateDiyPage(AppRainbowDiyPageSaveReqVO updateReqVO) {
        // 校验存在
        validateDiyPageExists(updateReqVO.getId());
        // 更新
        RainbowDiyPageDO updateObj = BeanUtils.toBean(updateReqVO, RainbowDiyPageDO.class);
        rainbowDiyPageMapper.updateById(updateObj);
    }

    @Override
    public void deleteDiyPage(Long id) {
        // 校验存在
        validateDiyPageExists(id);
        // 删除
        rainbowDiyPageMapper.deleteById(id);
    }

    private void validateDiyPageExists(Long id) {
        if (rainbowDiyPageMapper.selectById(id) == null) {
            throw exception(DIY_PAGE_NOT_EXISTS);
        }
    }

    @Override
    public RainbowDiyPageDO getDiyPage(Long id) {
        return rainbowDiyPageMapper.selectById(id);
    }

    @Override
    public PageResult<RainbowDiyPageDO> getDiyPagePage(AppRainbowDiyPagePageReqVO pageReqVO) {
        return rainbowDiyPageMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RainbowDiyPageDO> getDiyPageByTemplateId(Long templateId) {
        return rainbowDiyPageMapper.selectListByTemplateId(templateId);
    }

}
